<?php

namespace App\Http\Controllers\Admin;
use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view("admin.user.index");
    }
    //获取所有用户
    public function get_users(Request $request){
        $input = $request->input();
        $start = $input['page']*$input['limit']-$input['limit'];
        $end = $input['page']*$input['limit'];
        $data = [
            'code'=>0,
            'msg'=>'',
            'count'=>DB::table("user")->count(),
            'data'=>DB::table("user")->offset($start)->limit($end)->get()
        ];
        foreach($data['data'] as $v){
            if($v->tui_id == 0){
                $v->tui_id = '无推荐人';
            }else{
                $v->tui_id = DB::table("user")->where("user_id",$v->tui_id)->value("user_name");
            }
            $v->reg_time = date("Y-m-d H:i:s",$v->reg_time);
            $v->login_time = date("Y-m-d H:i:s",$v->login_time);
        }
        return $data;
    }
    //修改用的状态
    public function e_type(Request $request){
        $request = $request->input();
        $user_data = DB::table("user")->where("user_id",$request['user_id'])->first();
        if($request['name'] == 'type'){
            if($user_data->type == 1){
                DB::table("user")->where("user_id",$request['user_id'])->update(['type'=>0]);
                return '无效';
            }else{
                DB::table("user")->where("user_id",$request['user_id'])->update(['type'=>1]);
                return '有效';
            }
        }else{
            if($user_data->starts == 1){
                DB::table("user")->where("user_id",$request['user_id'])->update(['starts'=>0]);
                return '未激';
            }else{
                DB::table("user")->where("user_id",$request['user_id'])->update(['starts'=>1]);
                return '激活';
            }
        }
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        dd("添加");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
